<?php
/**
 * User: hufeng
 * Date: 2017/10/11
 * Desc: 权限管理
 */

namespace Admin\Controller;
use Model\CourseModel;
use Model\CourseTypeModel;

class CourseController extends BaseController{
    public $cModel;
    public $ctModel;
    public function __construct(){
        parent::__construct();
        $this->cModel = new CourseModel();
        $this->ctModel = new CourseTypeModel();
    }

    //课程列表
    public function index(){
        if($_POST['__hash__'] || !$_GET['p']){
            session('searchParam',null);
        }
        $sessionParam = session('searchParam');

        $sessionArr['status']  = @$_POST['status'] ? $_POST['status'] : @$sessionParam['status'];
        $sessionArr['name']  = @$_POST['name'] ? $_POST['name'] : @$sessionParam['name'];
        $sessionArr['start']  = @$_POST['start'] ? $_POST['start'] : @$sessionParam['start'];
        $sessionArr['end']  = @$_POST['end'] ? $_POST['end'] : @$sessionParam['end'];
        session('searchParam',$sessionArr);
        $page = @$_GET['p'] ? $_GET['p'] : 1;

        $where = [];
        $where['status'] = array('neq',3);
        if($sessionArr['status']){
            $where['status'] = $sessionArr['status'];
        }
        if($sessionArr['name']){
            $where['title'] = array('like','%'.$sessionArr['name'].'%');
        }
        if($sessionArr['start'] && $sessionArr['end']){
            $where['_string'] = "addtime>='{$sessionArr['start']}' and addtime<='{$sessionArr['end']} 23:59:59'";
        }
        $resList = $this->cModel->search($where,$page,$this->pernum);
        $totalNum = $this->cModel->getTotalNum($where);
        $pageShow = getPage($totalNum,$this->pernum);
        //所有分类
        $allType = $this->ctModel->allType();
        $this->assign('pageShow',$pageShow);
        $this->assign('page',$page);
        $this->assign('resList',$resList);
        $this->assign('totalNum',$totalNum);
        $this->assign('postArr',$sessionArr);
        $this->assign('allType',$allType);
        $this->display('course/index');
    }
    //新增课程
    public function addCourse(){
        if(IS_POST){
            $data['one_typeid'] = $_POST['onetype'];
            $data['two_typeid'] = $_POST['twotype'];
            $data['title'] = $_POST['title'];
            $data['info'] = $_POST['info'];
            $data['content'] = $_POST['content'];
            $data['cover_img'] = $_POST['cover_img'];
            $data['sort'] = $_POST['sort'];
            $data['addtime'] = $this->dateNow;
            $id = $this->cModel->addData($data);
            $this->logModel->log('添加课程','course',$id);
            $this->printOut(['code'=>0]);
        }
        //所有分类
        $allType = $this->ctModel->allType(0);
        $this->assign('allType',$allType);
        //获取分组
        $this->display('course/addcourse');
    }
    //编辑课程
    public function editCourse(){
        if(IS_POST){
            $where['id'] = $_POST['id'];
            $data['one_typeid'] = $_POST['onetype'];
            $data['two_typeid'] = $_POST['twotype'];
            $data['title'] = $_POST['title'];
            $data['info'] = $_POST['info'];
            $data['content'] = $_POST['content'];
            $data['cover_img'] = $_POST['cover_img'];
            $data['sort'] = $_POST['sort'];
            $data['addtime'] = $this->dateNow;
            $id = $this->cModel->editData($where,$data);
            $this->logModel->log('编辑课程','course',$id);
            $this->printOut(['code'=>0]);
        }

        //获取组内容
        $this->assign('page',$_GET['page']);
        $selfInfo = $this->cModel->findData(['id'=>$_GET['id']]);
        $this->assign('selfInfo',$selfInfo);
        //所有分类
        $allType = $this->ctModel->allType(0);
        $this->assign('allType',$allType);
        $this->display('course/editcourse');
    }
    //冻结解冻课程
    public function freezeCourse(){
        $data['status'] = $_GET['status'] == 1 ? 2: 1;
        $where['id'] = $_GET['id'];
        $this->cModel->editData($where,$data);
        $this->logModel->log('冻结|解冻课程','course',$_GET['id']);
        redirect($this->domianUrl.'/course/index?p='.$_GET['page'].'&tip=1');
    }
    //删除课程
    public function delCourse(){
        $data['status'] = 3;
        $where['id'] = $_GET['id'];
        $this->cModel->editData($where,$data);
        $this->logModel->log('删除课程','course',$_GET['id']);
        redirect($this->domianUrl.'/course/index?p='.$_GET['page'].'&tip=1');
    }
    //修改课程排序
    public function editCourseSort(){
        $data['sort'] = $_POST['sort'];;
        $where['id'] = $_POST['id'];
        $this->cModel->editData($where,$data);
        $this->logModel->log('修改课程排序','course',$_POST['id']);
        $this->printOut(['code'=>0]);
    }

    //课程分类列表
    public function type(){
        if($_POST['__hash__'] || !$_GET['p']){
            session('searchParam',null);
        }
        $sessionParam = session('searchParam');

        $sessionArr['name']  = @$_POST['name'] ? $_POST['name'] : @$sessionParam['name'];
        session('searchParam',$sessionArr);
        $page = @$_GET['p'] ? $_GET['p'] : 1;

        $where = [];
        $where['pid'] = 0;
        $where['status'] = 1;
        if($sessionArr['name']){
            $where['name'] = array('like','%'.$sessionArr['name'].'%');
        }
        $resList = $this->ctModel->search($where,$page,$this->pernum,'sort desc');
        $totalNum = $this->ctModel->getTotalNum($where);
        $pidArr = [];
        foreach ($resList as $val){
            $pidArr[] = $val['id'];
        }
        $childRes = [];
        if($pidArr){
            $childList = $this->ctModel->selectData(['pid'=>['in',$pidArr],'status'=>1]);
            foreach ($childList as $val){
                $childRes[$val['pid']][] = $val;
            }
        }

        $pageShow = getPage($totalNum,$this->pernum);
        $this->assign('pageShow',$pageShow);
        $this->assign('page',$page);
        $this->assign('resList',$resList);
        $this->assign('totalNum',$totalNum);
        $this->assign('postArr',$sessionArr);
        $this->assign('childRes',$childRes);
        $this->display('course/type');
    }
    //新增课程分类
    public function addType(){
        if(IS_POST){
            $data['name'] = $_POST['name'];
            $data['pid'] = $_POST['pid'];
            $data['addtime'] = date('Y-m-d H:i:s');
            $id = $this->ctModel->addData($data);
            $this->logModel->log('添加课程分类','course_type',$id);
            redirect($this->domianUrl.'/course/type?tip=1');
        }
        //所有父分类
        $pTypeList = $this->ctModel->selectData(['pid'=>0,'status'=>1]);
        $pTypeRes = [];
        foreach ($pTypeList as $val){
            $pTypeRes[$val['id']] = $val['name'];
        }
        //获取分组
        $this->assign('pTypeRes',$pTypeRes);
        $this->display('course/addtype');
    }
    //编辑课程分类
    public function editType(){
        if(IS_POST){
            $where['id'] = $_POST['id'];
            $data['name'] = $_POST['name'];
            $data['pid'] = $_POST['pid'];
            $this->ctModel->editData($where,$data);
            $this->logModel->log('编辑课程分类','course_type',$_POST['id']);
            redirect($this->domianUrl.'/course/type?p='.$_POST['page'].'&tip=1');
        }
        //获取组内容
        $this->assign('page',$_GET['page']);
        $selfInfo = $this->ctModel->findData(['id'=>$_GET['id']]);
        $this->assign('selfInfo',$selfInfo);

        //所有父分类
        $pTypeList = $this->ctModel->selectData(['pid'=>0,'status'=>1]);
        $pTypeRes = [];
        foreach ($pTypeList as $val){
            $pTypeRes[$val['id']] = $val['name'];
        }
        $this->assign('pTypeRes',$pTypeRes);
        $this->display('course/edittype');
    }
    //删除课程分类
    public function delType(){
        $data['status'] = 3;
        $where['id'] = $_GET['id'];
        $this->ctModel->editData($where,$data);
        $this->logModel->log('删除课程分类','course_type',$_GET['id']);
        redirect($this->domianUrl.'/course/type?p='.$_GET['page'].'&tip=1');
    }
    //修改分类排序
    public function editSort(){
        $data['sort'] = $_POST['sort'];;
        $where['id'] = $_POST['id'];
        $this->ctModel->editData($where,$data);
        $this->logModel->log('修改课程分类排序','course_type',$_POST['id']);
        $this->printOut(['code'=>0]);
    }
}